package g

import (
	"reflect"
	"testing"
)

func createList(l []int) *ListNode {
	last := &ListNode{}
	root := last
	for i := 0; i < len(l); i++ {
		last.Next = &ListNode{l[i], nil}
		last = last.Next
	}
	return root.Next
}

func Test_mergeKLists(t *testing.T) {
	type args struct {
		lists []*ListNode
	}
	tests := []struct {
		name string
		args args
		want *ListNode
	}{
		{"0", args{[]*ListNode{createList([]int{1}), createList([]int{0})}},
			createList([]int{0, 1})},
		{"1", args{nil}, nil},
		{"2", args{[]*ListNode{}}, nil},
		{"3", args{[]*ListNode{createList([]int{1, 4, 5}),
			createList([]int{1, 3, 4}), createList([]int{2, 6})}},
			createList([]int{1, 1, 2, 3, 4, 4, 5, 6})},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := mergeKLists(tt.args.lists)
			print(got, t)
			print(tt.want, t)
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("mergeKLists() = %v, want %v", got, tt.want)
			}
		})
	}
}

func print(p *ListNode, t *testing.T) {
	for p != nil {
		t.Logf("%v", p.Val)
		p = p.Next
	}
	t.Log("\n")
}
